/*
* Author: Sowmya Kameswaran
*/
package com.foo.SpatialData;

import javax.ejb.Stateless;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.foo.Entity.TickerInfoData;
import com.foo.TextDataSender.DataSender;


/**
 * Session Bean implementation class DataProvider
 */
@Stateless(name="SpatialDataProvider")
public class SpatialDataProvider implements SpatialDataProviderRemote, SpatialDataProviderLocal 
{

	@PersistenceContext(unitName="spatialinformationprovider") // This is the entity manager required by the Entity bean
	private EntityManager em;

	int total =0 ;
	Query getdatabylocation = null;
	List<TickerInfoData> datalist = null;
	String spatialmessage = ""; 
	DataSender datasender = new DataSender();
	/**
	 * Default constructor. 
	 */
	public SpatialDataProvider() 
	{
		/*try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("DataSender/remote");
			datasender = (DataSenderRemote) PortableRemoteObject.narrow(ref, DataSenderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
*/
		System.out.println("In server class to retrieve data \n"); 
	}

	@Override
	public void SpacialData(String sorttype,String value) 
	{
		System.out.println("Entering server function Spatial Data with parameters " +sorttype + " " +value);

		if(sorttype.equals("Country"))
		{
			System.out.println("Server side: if condition :");
			// Example for using named query
			spatialmessage = ""; 
			getdatabylocation = em.createQuery("from TickerInfoData d WHERE d.country = :country");
			getdatabylocation.setParameter("country", value);

			datalist = getdatabylocation.getResultList();
			System.out.println("Server side: list of data from db "+datalist);
			int listsize=datalist.size();
			System.out.println("list size "+listsize);
			for(int k=0 ; k<listsize ; k++)
			{
				spatialmessage += datalist.get(k).getId()+","+datalist.get(k).getTicker_symbol()+","+ datalist.get(k).getCompany() +"," +datalist.get(k).getSector_name() 
				+","+datalist.get(k).getIndustry()+ ","+datalist.get(k).getCountry()+","+datalist.get(k).getState()+","+datalist.get(k).getMarket_cap()+",";

			}
		}

		else if(sorttype.equals("State"))
		{
			spatialmessage = ""; 
			// Example for using named query
			value = value.trim();
			getdatabylocation = em.createQuery("from TickerInfoData d WHERE d.state = :state");
			getdatabylocation.setParameter("state", value);

			datalist = getdatabylocation.getResultList();
			int listsize=datalist.size();
			System.out.println("list size "+listsize);
			for(int k=0 ; k<listsize ; k++)
			{
				spatialmessage += datalist.get(k).getId()+","+datalist.get(k).getTicker_symbol()+","+ datalist.get(k).getCompany() +"," +datalist.get(k).getSector_name() 
				+","+datalist.get(k).getIndustry()+ ","+datalist.get(k).getCountry()+","+datalist.get(k).getState()+","+datalist.get(k).getMarket_cap()+",";

			}

		}
		else if(sorttype.equals("Industry"))
		{
			spatialmessage = ""; 
			// Example for using named query
			value = value.trim();
			getdatabylocation = em.createQuery("from TickerInfoData d WHERE d.industry = :industry");
			getdatabylocation.setParameter("industry", value);
			datalist = getdatabylocation.getResultList();

			int listsize=datalist.size();
			System.out.println("list size "+listsize);
			for(int k=0 ; k<listsize ; k++)
			{
				spatialmessage += datalist.get(k).getId()+","+datalist.get(k).getTicker_symbol()+","+ datalist.get(k).getCompany() +"," +datalist.get(k).getSector_name() 
				+","+datalist.get(k).getIndustry()+ ","+datalist.get(k).getCountry()+","+datalist.get(k).getState()+","+datalist.get(k).getMarket_cap()+",";

			}
		}
		else
			if(sorttype.equals("Sector"))
			{
				spatialmessage = ""; 
				// Example for using named query
				value = value.trim();
				getdatabylocation = em.createQuery("from TickerInfoData d WHERE d.sector_name = :sector_name");
				getdatabylocation.setParameter("sector_name", value);

				datalist = getdatabylocation.getResultList();
				int listsize=datalist.size();
				System.out.println("list size "+listsize);
				for(int k=0 ; k<listsize ; k++)
				{
					/*data.setId(datalist.get(k).getId());
					data.setTicker_symbol(datalist.get(k).getTicker_symbol());
					data.setCompany(datalist.get(k).getCompany());
					data.setSector_name(datalist.get(k).getSector_name());
					data.setIndustry(datalist.get(k).getIndustry());
					data.setCountry(datalist.get(k).getCountry());
					data.setState(datalist.get(k).getState());
					data.setMarket_cap(datalist.get(k).getMarket_cap());
					finaldata[total]=data;
					total++;*/
					spatialmessage += datalist.get(k).getId()+","+datalist.get(k).getTicker_symbol()+","+ datalist.get(k).getCompany() +"," +datalist.get(k).getSector_name() 
					+","+datalist.get(k).getIndustry()+ ","+datalist.get(k).getCountry()+","+datalist.get(k).getState()+","+datalist.get(k).getMarket_cap()+",";

				}

			}

		System.out.println("The message " +spatialmessage);
		datasender.sendMessage("Spatial",spatialmessage);
	}
	
}
